const path = require('path'); //使用绝对路径
const HtmlWebpackPlugin = require("html-webpack-plugin"); //打包html
const copyWebpackPlugin = require("copy-webpack-plugin"); //打包public

//配置webpack
module.exports = {
    //入口配置
    entry: path.resolve(__dirname, "../src/index.js"),

    //出口配置
    output: {
        path: path.resolve(__dirname, "../build"),
        filename: "./js/main.js",
        publicPath: "/"  //因为二级路由路径的变化,我们需要在webpack中的output中配置一个公共路径
    },

    //插件的配置
    plugins: [
        //生成html文件 并设置对应的参照模板
        new HtmlWebpackPlugin({
            template: path.resolve(__dirname, "../public/index.html")
        }),

        //复制public文件到打包目录，忽略index.html
        new copyWebpackPlugin({
            patterns: [{
                from: path.resolve(__dirname, "../public"),
                to: "public",
                globOptions: {
                    ignore: ["**/index.html"]
                }
            }],
        }),

    ],

    //loader的配置
    module: {
        rules: [
            //我们刚开始路由中渲染的是html类型，需要使用html-loader
            // {
            //     test: /\.html$/i,
            //     use: [
            //         'html-loader'
            //     ]
            // },
            {
                test: /\.ejs$/i,
                loader: 'ejs-loader',
                options: {
                    variable: 'data',
                }
            },
            //css的loader
            {
                test: /\.css$/i,
                use: [
                //use中loader加载是有顺序的，先下后上，注意有的loader需要按照顺序书写
                "style-loader",
                'css-loader'
                ]
            },
        ]
    },


    resolve: {
        //配置路径别名
        alias: {
            $v: path.resolve(__dirname, "../src/views"),
            $r: path.resolve(__dirname, "../src/routes"),
            $a: path.resolve(__dirname, "../src/api"),
        },

        //配置引入文件时 省略后缀,自动添加后缀的顺序
        extensions: [".js", ".json", ".ejs"]
    },

    //开发模式
    mode: "production",
}